﻿using System;
using System.Text;
using System.Text.RegularExpressions;

namespace MuzoBlog.Core.Helpers
{
    public class StringHelper
    {
        public static string Left(string input, int length)
        {
            return Left(input, length, string.Empty);
        }

        public static string Left(string input, int length, string subfix)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;

            StringBuilder stringBuilder = new StringBuilder(length);

            if (input.Length > length)
            {
                stringBuilder.Append(input.Substring(0, length));
                if (subfix != null)
                    stringBuilder.Append(subfix);
            }

            return stringBuilder.ToString();
        }

        public static string StripHtml(string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;

            return Regex.Replace(input, "((<[^>]*>)|(<(.|\\n)*?>))", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        }

        public static string ToSafeString(string input, int? length)
        {
            if (length.HasValue)
                input = Left(input, length.Value);

            input = Regex.Replace(input, "\r\n", Environment.NewLine);
            input = Regex.Replace(input, "[\\s]{2,}", " "); // spaces
            input = Regex.Replace(input, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); // br
            input = Regex.Replace(input, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // &nbsp;

            input = StripHtml(input);

            return input;
        }

        /// <summary>
        /// Search engine friendly string
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToSEFString(string input)
        {
            StringBuilder stringBuilder = new StringBuilder(input);
            stringBuilder.Replace("ç", "c");
            stringBuilder.Replace("ç", "c");
            stringBuilder.Replace("ö", "o");
            stringBuilder.Replace("ş", "s");
            stringBuilder.Replace("ü", "u");
            stringBuilder.Replace("ğ", "g");
            stringBuilder.Replace("ı", "i");
            stringBuilder.Replace("Ç", "C");
            stringBuilder.Replace("Ö", "O");
            stringBuilder.Replace("İ", "I");
            stringBuilder.Replace("Ş", "S");
            stringBuilder.Replace("Ü", "U");
            stringBuilder.Replace("Ğ", "G");
            stringBuilder.Replace(" ", "-");

            return Regex.Replace(stringBuilder.ToString(), "(-|\\W)+", "-");
        }

        public static bool IsWhiteSpace(string input)
        {
            if (string.IsNullOrEmpty(input))
                return false;

            for (int i = 0; i < input.Length; i++)
            {
                if (!Char.IsWhiteSpace(input[i]))
                    return false;
            }

            return true;
        }
    }
}